package com.cheung.leetcode.greedy;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.greedy
 * @Author :  cheungming
 * @CreateTime : 2024-06-05 07:11:45
 * @Description : 买卖股票的最佳时机 II
 * @Version : 1.0
 */
public class Code122Test {

    public int maxProfit(int[] prices) {
        // n天的总利润 = prices[n]-princes[0] = prices[n] - prices[0] + prices[n-1] - prices[0] + ... + prices[1] - prices[0]
        // 因此，求最大利润，就是只取后n-1项中的正差即可
        int count = 0;
        for (int i = 1; i < prices.length; i++) {
            count += Math.max(0, prices[i] - prices[i - 1]);
        }
        return count;
    }

    @Test
    public void test1() {
        int[] prices = new int[]{7, 1, 5, 3, 6, 4};
        assert maxProfit(prices) == 7;
    }

    @Test
    public void test2() {
        int[] prices = new int[]{1, 2, 3, 4, 5};
        assert maxProfit(prices) == 4;
    }

    @Test
    public void test3() {
        int[] prices = new int[]{7, 6, 4, 3, 1};
        assert maxProfit(prices) == 0;
    }

}
